<?php

/**
 * 台湾CMS系统 twCMS
 *
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at：2025.05.13
*/

namespace App\Http\Controllers\Api\Admin;

use App\Exceptions\JsonResponseException;
use App\Http\Controllers\Api\Controller;
use App\Services\User\Auth as AuthService;
use App\Services\User\ValidatorUser;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

/**
 * 后台管理
 */
class AuthController extends Controller
{
    /**
     * 登陆
     *
     * @param Request $request
     * @param ValidatorUser $validator
     * @param AuthService $auth
     * @param string $accountType
     * @return JsonResponse
     */
    public function postLogin(Request $request, ValidatorUser $validator, AuthService $auth, string $accountType): JsonResponse
    {
        try {
            // 数据验证
            $validated = $validator->login($request->all());
            $result = $auth->login($validated);
        }
        catch (ValidationException $e) {

            throw new JsonResponseException('failure', $e->errors(), 422);
        }

        return $this->sendResponse([], 'success');
    }

    /**
     * 退出
     *
     * @param AuthService $auth
     * @param string $accountType
     * @return JsonResponse
     */
    public function logout(AuthService $auth, string $accountType): JsonResponse
    {
        $auth->logout();

        return $this->sendResponse([], 'success');
    }
}
